package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜品分类接口实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    @Autowired
    public CategoryServiceImpl(CategoryMapper categoryMapper, DishMapper dishMapper, SetmealMapper setmealMapper) {
        this.categoryMapper = categoryMapper;
        this.dishMapper = dishMapper;
        this.setmealMapper = setmealMapper;
    }


    /**
     * 新增菜品分类
     *
     * @param categoryDTO 菜品分类实体
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //菜品分类默认禁用
        category.setStatus(StatusConstant.DISABLE);
        //设置修/创人id 时间
        //设置创建时间、修改时间、创建人、修改人
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(ThreadLocalUtil.get());
//        category.setUpdateUser(ThreadLocalUtil.get());
        categoryMapper.add(category);
    }

    /**
     * 分页查询菜品分类列表
     *
     * @param queryDTO 分页查询菜品分类列表实体
     * @return 菜品分类列表
     */
    @Override
    public PageResult<Category> pageCategoryList(CategoryPageQueryDTO queryDTO) {
        //启动分页
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());

        //根据mapper查询后的分页数据最后返回
        Page<Category> categories = categoryMapper.getCategoryList(queryDTO);
        return new PageResult<>(categories.getTotal(), categories.getResult());
    }

    /**
     * 启用/禁用菜品分类
     *
     * @param status 1启用 2禁用
     * @param id     修改的id
     */
    @Override
    public void startsOrStop(Integer status, Long id) {
        categoryMapper.update(Category.builder().id(id).status(status).build());
    }


    /**
     * 根据类型查询分类
     *
     * @param type //类型: 1菜品分类 2套餐分类
     * @return 类型下的所有菜品分类列表
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

    /**
     * 修改菜品分类
     *
     * @param categoryDTO 修改菜品分类实体
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(ThreadLocalUtil.get());
        categoryMapper.update(category);
    }


    /**
     * 根据id删除菜品分类
     *
     * @param id 菜品分类id
     */
    @Override
    public void deleteByCategoryId(Long id) {
        if (dishMapper.countByCategoryId(id) >= 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if (setmealMapper.countByCategoryId(id) >= 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }


}
